package com.benbi.shequapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.benbi.shequapi.bean.*;
import com.benbi.shequapi.mapper.*;
import com.benbi.shequapi.service.NewsService;
import com.benbi.shequapi.utils.Result;
import com.benbi.shequapi.utils.ResultData;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NewsImpl implements NewsService {
    @Resource
    NewsMapper newsMapper;


    @Override
    public Result getNewsList() {

        List<News> resultList;
        resultList = newsMapper.selectNewsList();
        if (resultList == null) {return new Result(1, "不存在新闻");}
        return new ResultData(resultList);
    }

    @Override
    public Result getNewsByNewsid(int news_id) {
        News p = newsMapper.selectNewsByNewsid(news_id);
        if (p == null) {return new Result(1, "不存在该新闻");}
        return new ResultData(p);
    }

    @Override
    public Result newNews(int a_pid,int top,String title,String content) {
        Date d = new Date();
        News n = new News(1,a_pid,top,title,content,d);
        int p = newsMapper.insertNews(n);
        return new Result(0, "成功");
    }

    @Override
    public Result deleteNews(int news_id){
        int p = newsMapper.deleteNewsByNewsid(news_id);
        return new Result(0, "成功");
    }



}
